import { Overlay } from "ol";

export class OverlayManager {
  constructor(map) {
    // this.overlayTools = OverlayTools.getInstance()
    this.mapContainerEle = map.getTargetElement();
    this.currentMap = map;
  }

  /**
   * 创建点坐标的小模态框
   * @param {*} vectorLayerConfig
   */
  createCustomOverlay(vectorLayerConfig) {
    const data = vectorLayerConfig["data"];
    data &&
      data.forEach((point) => {
        point["overlays"] &&
          point["overlays"].forEach((overlay) => {
            this.currentMap.addOverlay(new Overlay(overlay));
          });
      });
  }
  /**
   * 通过id移除overlay图层
   * @param {*} id 
   */
  removeOverlayByLayerId(id) {
    const overlayers = this.currentMap.getOverlays();
    for (let i = overlayers.getLength() - 1; i >= 0; i--) {
      if ((overlayers.item(i).getId() + "").indexOf(id) === 0) {
        overlayers.removeAt(i);
      }
    }
  }
  /**
   * overlay图层
   */
  removeAllOverlays() {
    console.log("移除所有覆盖层");
    this.currentMap.getOverlays().clear();
  }
  removeOverlayById(id) {
    const overlayers=this.currentMap.getOverlays()
    let currentIndex
    overlayers.forEach((item, index) => {
      if (item.getId() === id) {
        currentIndex = index
      }
    })
    if (currentIndex >= 0) {
      overlayers.removeAt(currentIndex)
    }
  }
}
